package com.wang.head.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.common.pojo.BasePojoEnum;
import com.wang.common.pojo.BaseQuery;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.web.controller.BaseController;
import com.wang.exam.pojo.AtExamDesc;
import com.wang.exam.pojo.AtExamDescEnum;
import com.wang.exam.pojo.AtExamDescQuery;
import com.wang.exam.pojo.AtExamIntvig;
import com.wang.exam.pojo.AtExamIntvigQuery;
import com.wang.exam.pojo.AtExamStat;
import com.wang.exam.pojo.AtExamStatQuery;
import com.wang.exam.pojo.AtNotice;
import com.wang.exam.pojo.AtNoticeQuery;
import com.wang.exam.service.AtExamDescService;
import com.wang.exam.service.AtExamIntvigService;
import com.wang.exam.service.AtExamStatService;
import com.wang.exam.service.AtNoticeService;
import com.wang.users.pojo.AtUsersDesc;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Objects;

/**
 * 示例的Controller
 *
 * @author ZjxMi
 */
@Controller
@RequestMapping("/head/exam")
@Log4j2
public class ExamHeadController extends BaseController {
    @Autowired
    private AtExamDescService examDescService;
    @Autowired
    private AtExamStatService examStatService;
    @Autowired
    private AtExamIntvigService examIntvigService;
    @Autowired
    private AtNoticeService noticeService;

    /**
     * 考试概要查询多条
     *
     * @return
     */
    @RequestMapping("/examDescList")
    public String examDescList(AtExamDescQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==考试概要列表==");
        AtUsersDesc usersDescSess = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        /* 分页对象 */
        IPage<AtExamDesc> paramPage = null;
        try {
            paramPage = new Page<>(paramQuery.getCurrent(), paramQuery.getSize());
        } catch (Exception e) {
            paramPage = new Page<>(1, 20);
        }
        /* 时间处理 */
        if (StringUtils.isNotEmpty(paramQuery.getStDate())) {
            paramQuery.setPubTimeSt(this.dateUtil.strToDateTime(paramQuery.getStDate()));
        }
        paramQuery.addLike();

        /* 增加条件 */
        AtExamDesc entity = paramQuery.getEntity();
        if (AtExamDescQuery.FINAL_SELF_OBJ_TRUE.equalsIgnoreCase(paramQuery.getSelfObj())) {
            entity.setIntvigMainId(usersDescSess.getId());
        } else {
            /* 只能查询审核通过的数据 */
            entity.setStatus(AtExamDescEnum.STATUSNEW_PASS.getCode());
        }

        /* 查询对象 */
        List<AtExamDesc> dataList = this.examDescService.findList(paramPage, paramQuery);
        paramQuery.removeLike();
        /* 列表数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_DATALIST, dataList);
        /* 分页数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PAGEINFOUTIL, paramPage);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ENUMSONE, new AtExamDesc());
        /* 查询数据 */
        return "/head/exam/examDescList";
    }

    /**
     * 考试概要打开添加页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/examDescInsert")
    public String examDescInsert(AtExamDescQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==考试概要添加==");
        AtExamDesc entity = paramQuery.getEntity();
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, entity);
        /* 查询数据 */
        return "/head/exam/examDescInsert";
    }

    /**
     * 考试概要打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/examDescInsertSubmit")
    public String examDescInsertSubmit(AtExamDescQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==考试概要添加提交==");
        AtExamDesc entity = paramQuery.getEntity();
        /* 从session中获取信息 */
        AtUsersDesc one = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        if (Objects.nonNull(one)) {
            entity.setIntvigMainId(one.getId());
        }
        if (StringUtils.isNotEmpty(paramQuery.getCurrDate())) {
            entity.setCurrDate(this.dateUtil.strToDate(paramQuery.getCurrDate()));
        }
        boolean dbFlag = this.examDescService.save(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        model.addAttribute(ConstatFinalUtil.FINAL_RESPONSE, resultJson);
        /* 查询数据 */
        return this.examDescInsert(paramQuery, model, request);
    }

    /**
     * 考试概要打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/examDescUpdate")
    public String examDescUpdate(AtExamDescQuery paramQuery, Model model) {
        log.info("==考试概要更新==");
        /* 根据id查询 */
        AtExamDesc one = this.examDescService.findOne(paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, one);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        if (BaseQuery.FINAL_OPER_UPDATE.equalsIgnoreCase(paramQuery.getOperType())) {
            /* 查询数据 */
            return "/head/exam/examDescUpdate";
        }
        /* 查询数据 */
        return "/head/exam/examDescInfo";
    }

    /**
     * 考试概要打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/examDescUpdateSubmit")
    public String examDescUpdateSubmit(AtExamDescQuery paramQuery, Model model) {
        log.info("==考试概要更新提交==");
        AtExamDesc entity = paramQuery.getEntity();
        if (StringUtils.isNotEmpty(paramQuery.getPubTime())) {
            entity.setPubTime(this.dateUtil.strToDateTime(paramQuery.getPubTime()));
        }
        if (StringUtils.isNotEmpty(paramQuery.getCurrDate())) {
            entity.setCurrDate(this.dateUtil.strToDate(paramQuery.getCurrDate()));
        }
        /* 根据id查询 */
        boolean dbFlag = this.examDescService.updateById(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        model.addAttribute(ConstatFinalUtil.FINAL_RESPONSE, resultJson);
        /* 查询数据 */
        return this.examDescUpdate(paramQuery, model);
    }

    /**
     * 监考记录查询多条
     *
     * @return
     */
    @RequestMapping("/intvigList")
    public String intvigList(AtExamIntvigQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==监考记录列表==");
        AtUsersDesc usersDescSess = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        /* 分页对象 */
        IPage<AtExamIntvig> paramPage = null;
        try {
            paramPage = new Page<>(paramQuery.getCurrent(), paramQuery.getSize());
        } catch (Exception e) {
            paramPage = new Page<>(1, 20);
        }
        /* 时间处理 */
        if (StringUtils.isNotEmpty(paramQuery.getStDate())) {
            paramQuery.setPubTimeSt(this.dateUtil.strToDateTime(paramQuery.getStDate()));
        }
        paramQuery.addLike();

        /* 增加条件 */
        AtExamIntvig entity = paramQuery.getEntity();
        if (AtExamIntvigQuery.FINAL_SELF_OBJ_TRUE.equalsIgnoreCase(paramQuery.getSelfObj())) {
            entity.setUsersId(usersDescSess.getId());
        }

        /* 查询对象 */
        List<AtExamIntvig> dataList = this.examIntvigService.findList(paramPage, paramQuery);
        paramQuery.removeLike();
        /* 列表数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_DATALIST, dataList);
        /* 分页数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PAGEINFOUTIL, paramPage);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ENUMSONE, new AtExamIntvig());
        /* 查询数据 */
        return "/head/exam/intvigList";
    }

    /**
     * 监考记录打开添加页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/intvigInsert")
    public String intvigInsert(AtExamIntvigQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==监考记录添加==");
        AtExamIntvig entity = paramQuery.getEntity();
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, entity);
        /* 查询数据 */
        return "/head/exam/intvigInsert";
    }

    /**
     * 监考记录打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/intvigInsertSubmit")
    public String intvigInsertSubmit(AtExamIntvigQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==监考记录添加提交==");
        AtExamIntvig entity = paramQuery.getEntity();
        /* 从session中获取信息 */
        AtUsersDesc one = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        if (Objects.nonNull(one)) {
            entity.setUsersId(one.getId());
        }
        boolean dbFlag = this.examIntvigService.save(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        model.addAttribute(ConstatFinalUtil.FINAL_RESPONSE, resultJson);
        /* 查询数据 */
        return "redirect:/head/exam/intvigList";
    }

    /**
     * 监考记录打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/intvigUpdate")
    public String intvigUpdate(AtExamIntvigQuery paramQuery, Model model) {
        log.info("==监考记录更新==");
        /* 根据id查询 */
        AtExamIntvig one = this.examIntvigService.findOne(paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, one);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        if (BaseQuery.FINAL_OPER_UPDATE.equalsIgnoreCase(paramQuery.getOperType())) {
            /* 查询数据 */
            return "/head/exam/intvigUpdate";
        }
        /* 查询数据 */
        return "/head/exam/intvigInfo";
    }

    /**
     * 监考记录打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/intvigUpdateSubmit")
    public String intvigUpdateSubmit(AtExamIntvigQuery paramQuery, Model model) {
        log.info("==监考记录更新提交==");
        AtExamIntvig entity = paramQuery.getEntity();
        if (StringUtils.isNotEmpty(paramQuery.getPubTime())) {
            entity.setPubTime(this.dateUtil.strToDateTime(paramQuery.getPubTime()));
        }
        /* 根据id查询 */
        boolean dbFlag = this.examIntvigService.updateById(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        model.addAttribute(ConstatFinalUtil.FINAL_RESPONSE, resultJson);
        /* 查询数据 */
        return this.intvigUpdate(paramQuery, model);
    }

    /**
     * 统计查询多条
     *
     * @return
     */
    @RequestMapping("/statList")
    public String statList(AtExamStatQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==统计列表==");
        AtUsersDesc usersDescSess = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        /* 分页对象 */
        IPage<AtExamStat> paramPage = null;
        try {
            paramPage = new Page<>(paramQuery.getCurrent(), paramQuery.getSize());
        } catch (Exception e) {
            paramPage = new Page<>(1, 20);
        }
        /* 时间处理 */
        if (StringUtils.isNotEmpty(paramQuery.getStDate())) {
            paramQuery.setPubTimeSt(this.dateUtil.strToDateTime(paramQuery.getStDate()));
        }
        paramQuery.addLike();

        /* 增加条件 */
        AtExamStat entity = paramQuery.getEntity();

        /* 查询对象 */
        List<AtExamStat> dataList = this.examStatService.findList(paramPage, paramQuery);
        paramQuery.removeLike();
        /* 列表数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_DATALIST, dataList);
        /* 分页数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PAGEINFOUTIL, paramPage);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ENUMSONE, new AtExamStat());
        /* 查询数据 */
        return "/head/exam/statList";
    }

    /**
     * 统计打开添加页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/statInsert")
    public String statInsert(AtExamStatQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==统计添加==");
        AtExamStat entity = paramQuery.getEntity();
        String templateId = "";
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, entity);
        /* 查询数据 */
        return "/head/exam/statInsert";
    }

    /**
     * 统计打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/statInsertSubmit")
    public String statInsertSubmit(AtExamStatQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==统计添加提交==");
        AtExamStat entity = paramQuery.getEntity();
        /* 从session中获取信息 */
        AtUsersDesc one = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        if (Objects.nonNull(one)) {
            entity.setUsersId(one.getId());
        }
        boolean dbFlag = this.examStatService.save(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        model.addAttribute(ConstatFinalUtil.FINAL_RESPONSE, resultJson);
        /* 查询数据 */
        return this.statInsert(paramQuery, model, request);
    }

    /**
     * 统计打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/statUpdate")
    public String statUpdate(AtExamStatQuery paramQuery, Model model) {
        log.info("==统计更新==");
        /* 根据id查询 */
        AtExamStat one = this.examStatService.findOne(paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, one);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        if (BaseQuery.FINAL_OPER_UPDATE.equalsIgnoreCase(paramQuery.getOperType())) {
            /* 查询数据 */
            return "/head/exam/statUpdate";
        }
        /* 查询数据 */
        return "/head/exam/statInfo";
    }

    /**
     * 统计打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/statUpdateSubmit")
    public String statUpdateSubmit(AtExamStatQuery paramQuery, Model model) {
        log.info("==统计更新提交==");
        AtExamStat entity = paramQuery.getEntity();
        if (StringUtils.isNotEmpty(paramQuery.getPubTime())) {
            entity.setPubTime(this.dateUtil.strToDateTime(paramQuery.getPubTime()));
        }
        /* 根据id查询 */
        boolean dbFlag = this.examStatService.updateById(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        model.addAttribute(ConstatFinalUtil.FINAL_RESPONSE, resultJson);
        /* 查询数据 */
        return this.statUpdate(paramQuery, model);
    }

    /**
     * 公告查询多条
     *
     * @return
     */
    @RequestMapping("/noticeList")
    public String noticeList(AtNoticeQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==公告列表==");
        AtUsersDesc usersDescSess = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        /* 分页对象 */
        IPage<AtNotice> paramPage = null;
        try {
            paramPage = new Page<>(paramQuery.getCurrent(), paramQuery.getSize());
        } catch (Exception e) {
            paramPage = new Page<>(1, 20);
        }
        /* 时间处理 */
        if (StringUtils.isNotEmpty(paramQuery.getStDate())) {
            paramQuery.setPubTimeSt(this.dateUtil.strToDateTime(paramQuery.getStDate()));
        }
        paramQuery.addLike();

        /* 增加条件 */
        AtNotice entity = paramQuery.getEntity();

        /* 查询对象 */
        List<AtNotice> dataList = this.noticeService.findList(paramPage, paramQuery);
        paramQuery.removeLike();
        /* 列表数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_DATALIST, dataList);
        /* 分页数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PAGEINFOUTIL, paramPage);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ENUMSONE, new AtNotice());
        /* 查询数据 */
        return "/head/exam/noticeList";
    }

    /**
     * 公告打开添加页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/noticeInsert")
    public String noticeInsert(AtNoticeQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==公告添加==");
        AtNotice entity = paramQuery.getEntity();
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, entity);
        /* 查询数据 */
        return "/head/exam/noticeInsert";
    }

    /**
     * 公告打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/noticeInsertSubmit")
    public String noticeInsertSubmit(AtNoticeQuery paramQuery, Model model, HttpServletRequest request) {
        log.info("==公告添加提交==");
        AtNotice entity = paramQuery.getEntity();
        /* 从session中获取信息 */
        AtUsersDesc one = (AtUsersDesc) this.findObjFromSession(request, BasePojoEnum.SESS_USERS);
        boolean dbFlag = this.noticeService.save(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        model.addAttribute(ConstatFinalUtil.FINAL_RESPONSE, resultJson);
        /* 查询数据 */
        return this.noticeInsert(paramQuery, model, request);
    }

    /**
     * 公告打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/noticeUpdate")
    public String noticeUpdate(AtNoticeQuery paramQuery, Model model) {
        log.info("==公告更新==");
        /* 根据id查询 */
        AtNotice one = this.noticeService.findOne(paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, one);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        if (BaseQuery.FINAL_OPER_UPDATE.equalsIgnoreCase(paramQuery.getOperType())) {
            /* 查询数据 */
            return "/head/exam/noticeUpdate";
        }
        /* 查询数据 */
        return "/head/exam/noticeInfo";
    }

    /**
     * 公告打开更新页面
     * 更新和详情是同一个页面;
     *
     * @return
     */
    @RequestMapping("/noticeUpdateSubmit")
    public String noticeUpdateSubmit(AtNoticeQuery paramQuery, Model model) {
        log.info("==公告更新提交==");
        AtNotice entity = paramQuery.getEntity();
        if (StringUtils.isNotEmpty(paramQuery.getPubTime())) {
            entity.setPubTime(this.dateUtil.strToDateTime(paramQuery.getPubTime()));
        }
        /* 根据id查询 */
        boolean dbFlag = this.noticeService.updateById(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        model.addAttribute(ConstatFinalUtil.FINAL_RESPONSE, resultJson);
        /* 查询数据 */
        return this.noticeUpdate(paramQuery, model);
    }
}
